package com.qf.mvc2203.controller;

import com.qf.mvc2203.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/param")
public class ParamController {
    /*
    演示 普通参数收参
     http://localhost:8080/param/test1?id=1&name=zzz&gender=false&birth=2018-12-12 12:20:30
     注意，如果是日期类型收参，需要用斜线分隔年月日，直接使用Date类型接收即可
     http://localhost:8080/param/test1?id=1&name=zzz&gender=false&birth=2018/12/12 12:20:30
     */
    @RequestMapping("/test1")
    public String test1(Integer id, String name, Boolean gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date birth){
        // 接收参数并打印
        System.out.println(id);
        System.out.println(name);
        System.out.println(gender);
        System.out.println(birth);
        return "/hi.html";
    }
    /*
      演示 实体参数收参
     http://localhost:8080/param/test2?id=1&name=zzz&gender=false&birth=2018-12-12 12:20:30
     注意，如果是日期类型收参，需要用斜线分隔年月日，直接使用Date类型接收即可
     http://localhost:8080/param/test2?id=1&name=zzz&gender=false&birth=2018/12/12 12:20:30
     */
    @RequestMapping("/test2")
    public String test2(User user){
        // 接收参数并打印
        System.out.println(user);
        return "/hi.html";
    }
    /*
        数组收参
     */
    @RequestMapping("/test3")
    public String test3(String[] hobby){
        for (String s : hobby) {
            System.out.println(s);
        }
        return "/hi.html";
    }
    /*
    数组收参
     */
    @RequestMapping("/test4")
    public String test4(User user){
        System.out.println(user);
        for (String s : user.getHobby()) {
            System.out.println(s);
        }
        return "/hi.html";
    }
    /*
    路径收参
     */
    @RequestMapping("/get/{id}")
    public String test5(@PathVariable("id") Integer id){
        System.out.println(id);
        return "/hi.html";
    }
    /*
 路径收参
  */
    @RequestMapping("/get/{name}/{password}")
    public String test6(@PathVariable("name") String name,
                        @PathVariable("password") String password){
        System.out.println(name);
        System.out.println(password);
        return "/hi.html";
    }
    @RequestMapping("/get/{name}/user/{password}")
    public String test7(@PathVariable("name") String name,
                        @PathVariable("password") String password){
        System.out.println(name);
        System.out.println(password);
        return "/hi.html";
    }
}
